﻿Public Class frmPurchase
    Dim dtp As DataTable
    Public itemid = 0
    Public total = 0
    Private Sub dgvlist_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvlist.RowPrePaint
        If e.RowIndex >= 0 Then
            Me.dgvlist.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
        End If
    End Sub
    'Private Sub checkpay_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkpay.CheckedChanged
    '    If checkpay.Checked = False Then
    '        GroupPay.Visible = True
    '    Else
    '        GroupPay.Visible = False
    '    End If
    'End Sub

    Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click
        Dim frm As New frmViewProduct
        frm.ShowDialog()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        loadpayment()
        loademp()
        loadvendor()
        UI.FormatDataGrid(dgvlist)
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Private Sub loadpayment()
        dtp = Cmd.ExecuteDataTable("select * from tbl_exchange_rate_detail where exchange_rate_id=1")
        cboPayMethod.DisplayMember = "currency_name"
        cboPayMethod.ValueMember = "exchange_rate_detail_id"
        cboPayMethod.DataSource = dtp
    End Sub

    Private Sub txtamount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtamount.TextChanged
        txtowe.Text = Val(lblTotal.Text) - Val(txtamount.Text)
    End Sub


    Private Sub txtqty_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtqty.KeyDown
        If e.KeyCode = Keys.Enter Then
            addRow()
            txtcode.Text = ""
            txtcode.Focus()
        End If
    End Sub

    Sub clearitem()
        txtitemname.Text = "N/A"
        txtqty.Text = "1"
        txtprice.Text = "0"
        txtdiscount.Value = 0
        itemid = 0
        total = 0
        picitem.Image = Nothing
    End Sub

    Private Sub addRow()
        If itemid <> 0 Then
            Dim i = Me.dgvlist.Rows.Add
            Dim row = Me.dgvlist.Rows(i)
            total = (Val(txtqty.Text) * Val(txtprice.Text))
            total = CInt(total - ((total * Val(txtdiscount.Value)) / 100))
            row.Cells("item_id").Value = itemid
            row.Cells("item_name").Value = txtitemname.Text
            row.Cells("item_price").Value = txtprice.Text
            row.Cells("item_qty").Value = txtqty.Text
            row.Cells("amount").Value = total
            row.Cells("item_discount").Value = txtdiscount.Value
            row.Cells("subamount").Value = Val(txtqty.Text) * Val(txtprice.Text)
            lblsubtotal.Text = CInt(Val(lblsubtotal.Text) + total)

            lblTotal.Text = CInt(Val(lblsubtotal.Text) - (Val(lblsubtotal.Text) * (Val(txttotaldisc.Value) / 100)))

            txtowe.Text = Val(lblTotal.Text) - Val(txtamount.Text)
            clearitem()
        End If
        
    End Sub
    Private Sub txttotaldisc_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttotaldisc.ValueChanged
        lblTotal.Text = CInt(Val(lblsubtotal.Text) - (Val(lblsubtotal.Text) * (Val(txttotaldisc.Value) / 100)))
    End Sub

    Private Sub txtprice_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtprice.KeyDown
        If e.KeyCode = Keys.Enter Then
            addRow()
            txtcode.Text = ""
            txtcode.Focus()
        End If
    End Sub

    Private Sub txtdiscount_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtdiscount.KeyDown
        If e.KeyCode = Keys.Enter Then
            addRow()
            txtcode.Text = ""
            txtcode.Focus()
        End If
    End Sub

    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        Dim row As DataGridViewRow

        If Not (dgvlist.SelectedRows.Count < 1) Then
            row = dgvlist.SelectedRows(0)
            lblsubtotal.Text = CInt(Val(lblsubtotal.Text) - row.Cells("amount").Value)
            lblTotal.Text = CInt(Val(lblsubtotal.Text) - (Val(lblsubtotal.Text) * (Val(txttotaldisc.Value) / 100)))
            Me.dgvlist.Rows.Remove(row)
            txtowe.Text = Val(lblTotal.Text) - Val(txtamount.Text)
            ' Else
            ' MessageBox.Show("ABC")
        End If

    End Sub

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
        Dim row As DataGridViewRow
        row = dgvlist.SelectedRows(0)
        Me.dgvlist.Rows.Clear()
        lblsubtotal.Text = "0"
        lblTotal.Text = "0"
        txtowe.Text = "0"
    End Sub

    Private Sub cboPayMethod_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPayMethod.SelectedIndexChanged
        lblp1.Text = cboPayMethod.Text
        lblp3.Text = cboPayMethod.Text
    End Sub
   
    Private Sub savePurchase() Handles btnsave.Click
        If txtref.Text.Trim = "" Then
            myMsgbox.show("Reference Invoice cannot be empty!!", "New Purchase")
            Exit Sub
        End If
        ClassPurchase.addPurchase(txtref.Text, cbovendor.SelectedValue, cboemp.SelectedValue, 0, cboPayMethod.SelectedValue, lblsubtotal.Text, txttotaldisc.Value, txtvat.Value, lblTotal.Text, Now.Date, dgvlist, "", 1, txtamount.Text)
        clearitem()
        dgvlist.Rows.Clear()
        txtref.Text = ""
        txttotaldisc.Value = 0
        txtvat.Value = 0
        lblsubtotal.Text = "0"
        lblTotal.Text = "0"
        txtowe.Text = "0"
        txtamount.Text = 0

    End Sub

    Private Sub loadvendor()
        cbovendor.DisplayMember = "supplier_name"
        cbovendor.ValueMember = "supplier_id"
        cbovendor.DataSource = Cmd.ExecuteDataTable("select supplier_id, supplier_name from tbl_supplier where active=1")
    End Sub
    Private Sub loademp()
        cboemp.DisplayMember = "employee_name"
        cboemp.ValueMember = "employee_id"
        cboemp.DataSource = Cmd.ExecuteDataTable("select employee_id, employee_name from tbl_employee where active=1")
    End Sub

    Private Sub txtcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcode.TextChanged
        If loadItem() Then
            txtqty.Focus()
            txtqty.SelectAll()
        End If
    End Sub
    Private Function loadItem() As Boolean
        Dim dt As New DataTable
        Try
            Cmd.Parameters("@search") = txtcode.Text
            dt = Cmd.ExecuteDataTable("select i.item_id,new_price ,i.item_name,i.photo from tbl_stock s inner join tbl_item i on i.item_id=s.item_id  where i.item_code=@search and i.active= true")
            itemid = dt.Rows(0).Item("item_id")
            txtitemname.Text = dt.Rows(0).Item("item_name")
            txtprice.Text = dt.Rows(0).Item("new_price")
            Dim bytes As Byte() = DirectCast(dt.Rows(0).Item("photo"), Byte())
            Dim m As New System.IO.MemoryStream(bytes)
            picitem.Image = New System.Drawing.Bitmap(m)
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Sub newItem() Handles btnnewitem.Click
        Dim frm As New frmNewProduct
        Dim dtitem As New DataTable
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            dtitem = Cmd.ExecuteDataTable("SELECT item_id,item_name,photo from tbl_item where item_id=(select MAX(item_id) from tbl_item)")

            itemid = dtitem.Rows(0).Item("item_id")
            txtitemname.Text = dtitem.Rows(0).Item("item_name")
            Dim bytes As Byte() = DirectCast(dtitem.Rows(0).Item("photo"), Byte())
            Dim m As New System.IO.MemoryStream(bytes)
            picitem.Image = New System.Drawing.Bitmap(m)

            txtqty.SelectAll()
            txtqty.Focus()
        End If
    End Sub

    Private Sub cbovendor_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbovendor.SelectedIndexChanged
        txtvendornum.Text = Cmd.ExecuteScalar("select phone from tbl_supplier where supplier_id='" & cbovendor.SelectedValue & "'")
    End Sub

    Private Sub savePurchase(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click

    End Sub

    Private Sub cboemp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboemp.SelectedIndexChanged
        txtempnum.Text = Cmd.ExecuteScalar("select phone from tbl_employee where employee_id='" & cboemp.SelectedValue & "'")
    End Sub
End Class